<?php

namespace addons\TfFutures\handler;

use linslin\yii2\curl;
use addons\TfFutures\common\models\Symbol;
use yii\db\Expression;

class GatherAliyun extends Gather
{
    public static $tmpModelList = null;

    public function run($limits)
    {

        $time = time();
        $stime = strtotime(date('Y-m-d ') . ' 09:25:00');
        $etime = strtotime(date('Y-m-d ') . ' 15:01:00');
        if ($time > $stime && $time < $etime) {
        $query = Symbol::find()->where(['on_sale' => 'on'])->orderBy('id desc');
        $tmpModelList = [];
        //echo $offset.'==>'.$limit.PHP_EOL;
        $_tmpModelList = $query->asArray()->all();
        if (!empty($_tmpModelList)) {
            foreach ($_tmpModelList as $tmpModel) {
                $tmpModelList[$tmpModel['code2']] = $tmpModel;
            }
            $symbol = [];
            foreach ($tmpModelList as $code2 => $tmpModel) {
                $symbol[] = $code2;
                
                if (count($symbol) > 9) {
                    $symbol = implode(',', $symbol);
                    $curl = new curl\Curl();
                    $curl = $curl->setHeaders(['Authorization' => 'APPCODE 94463dd6a31b45779c17832c4d3992e9']);
                    $resp = $curl->get("http://alirm-com.konpn.com/query/comrms?symbols={$symbol}");
                    
                    if ($curl->errorCode == null) {
                        $resp = json_decode($resp, true);
                        if (!empty($resp['Obj'])) {
                            foreach ($resp['Obj'] as $symbol) {
                                if (!empty($tmpModelList[$symbol['FS']])) {
                                    $ticket = empty($tmpModelList[$symbol['FS']]['ticekt']) ? [] : $tmpModelList[$symbol['FS']]['ticekt'];
                                    $ticket['ask'] = $symbol['B1'];
                                    $ticket['asm'] = $symbol['B1V'];
                                    $ticket['ask2'] = $symbol['B2'];
                                    $ticket['asm2'] = $symbol['B2V'];
                                    $ticket['ask3'] = $symbol['B3'];
                                    $ticket['asm3'] = $symbol['B3V'];
                                    $ticket['ask4'] = $symbol['B4'];
                                    $ticket['asm4'] = $symbol['B4V'];
                                    $ticket['ask5'] = $symbol['B5'];
                                    $ticket['asm5'] = $symbol['B5V'];
                                    $ticket['bid'] = $symbol['S1'];
                                    $ticket['bim'] = $symbol['S1V'];
                                    $ticket['bid2'] = $symbol['S2'];
                                    $ticket['bim2'] = $symbol['S2V'];
                                    $ticket['bid3'] = $symbol['S3'];
                                    $ticket['bim3'] = $symbol['S3V'];
                                    $ticket['bid4'] = $symbol['S4'];
                                    $ticket['bim4'] = $symbol['S4V'];
                                    $ticket['bid5'] = $symbol['S5'];
                                    $ticket['bim5'] = $symbol['S5V'];
                                    $ticket['open'] = $symbol['O'];
                                    $ticket['close'] = $symbol['YC'];
                                    $ticket['high'] = $symbol['H'];
                                    $ticket['low'] = $symbol['L'];

                                    $ticket['price'] = $symbol['P'];
                                    $ticket['volume'] = $symbol['B1V']+$symbol['S1V'];
                                    $ticket['position'] = $symbol['HD'];
                                    $ticket['limit_up'] = 0;
                                    $ticket['limit_dn'] = 0;
                                    $ticket['cleaning'] = 0;
                                    $ticket['close_past'] = $symbol['YC'];//昨收
                                    $ticket['position_past'] = $symbol['YJS'];//昨持
                                    $ticket['cleaning_past'] = $symbol['YHD'];//昨结

                                    $ticket['tm'] = $symbol['Tick'];
                                    $ticket['ctm'] = $symbol['Tick'];
                                    $ticket['ctmfmt'] = date('Y-m-d H:i:s', $symbol['Tick']);
                                    $ticket['wave'] = $symbol['ZF'];
                                    $tmpModelList[$symbol['FS']]['ticekt'] = $ticket;
                                    
                                }
                            }
                        }
                    }
                    $symbol = [];
                }
            
            	sleep(0.5);
            }
            $this->insert($tmpModelList);
        }
        }
    }

}